define(["angularAMD"], function (angularAMD) {
    angularAMD.service('handleLogService', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog){
            var _service = this;
            function GetReasonList(data, callback){
                var query_condition_info = [];
                query_condition_info.push({column_name:'ReasonSubType', query_mode:'0', value:'COL', merge_condition_model:'AND' });
                query_condition_info.push({column_name:'ReasonType', query_mode:'0', value:10, merge_condition_model:'AND' });
                query_condition_info.push({column_name:'ReasonNo', query_mode:'0', value:data.reason_no, merge_condition_model:'AND' });
                $MMWService.pageNumberInfoGet({
                    page_mode : 8,
                    query_condition_info: query_condition_info
                }, function(items){
                    if(typeof callback === 'function')
                        callback(items);
                });
            }
            _service.init = function(options){
                $mdDialog.dialog(
                    'module/moduleE/program/E07/handleLog.tmp.html',
                    function(dialog){
                        return {
                            css_url:'module/moduleE/program/E07/handleLog.css',
                            itemList:angular.copy(options.itemList),
                            data:{
                                reason_no:'',
                                reason_name:'',
                                desc:''
                            },
                            beforeShown: function(){

                            },
                            loadReason: function(){
                                $MMWService.showQueryListPNIG({
                                    page_mode: 8,
                                    condition: [
                                        {name:$filter('translate')('common.queryList.badReason_c'), column_name:'ReasonNo', query_mode:'2', value:dialog.data.reason_no}
                                    ],
                                    header: [
                                        {label:$filter('translate')('common.queryList.badReason_c'), code:'page_no'},
                                        {label:$filter('translate')('common.queryList.badReason_n'), code:'page_name'}
                                    ],
                                    setCondition:function(conditions){
                                        conditions.push({column_name:'ReasonSubType', query_mode:'0', value:'COL', merge_condition_model:'AND' });
                                        conditions.push({column_name:'ReasonType', query_mode:'0', value:10, merge_condition_model:'AND' });
                                        return conditions;
                                    },
                                    selectCode: dialog.data.reason_no,
                                    confirm : function(selectItem){
                                        dialog.data.reason_no = selectItem.page_no;
                                        dialog.data.reason_name = selectItem.page_name;
                                    }
                                });
                            },
                            addReason: function(){
                                if(dialog.data.reason_no == ''){
                                    //請輸入原因編號
                                    $rootScope.showAlert($filter('translate')('E07.msg.reason_empty'));
                                } else {
                                    if(dialog.data.reason_name == ''){
                                        GetReasonList({
                                            reason_no: dialog.data.reason_no
                                        }, function(items){
                                            if(items.length > 0){
                                                dialog.itemList.push({
                                                    reason_no: dialog.data.reason_no,
                                                    reason_name: dialog.data.reason_name,
                                                    desc: dialog.data.desc
                                                });
                                                dialog.data = {
                                                    reason_no:'',
                                                    reason_name:'',
                                                    desc:''
                                                };
                                            } else {
                                                $rootScope.showAlert($filter('translate')('E07.msg.reason_not_exist'));
                                            }
                                        });
                                    } else {
                                        dialog.itemList.push({
                                            reason_no: dialog.data.reason_no,
                                            reason_name: dialog.data.reason_name,
                                            desc: dialog.data.desc
                                        });
                                        dialog.data = {
                                            reason_no:'',
                                            reason_name:'',
                                            desc:''
                                        };
                                    }
                                }
                            },
                            removeReason: function(item){
                                dialog.itemList.splice(dialog.itemList.indexOf(item), 1);
                            },
                            back: function () {
                                dialog.hide();
                                if(options.close)
                                    options.close();
                            },
                            confirm: function () {
                                if(typeof(options.callback) == "function")
                                    options.callback(dialog);
                            }
                        };
                    }
                );
            }
        }
    ]);
});
